package com.eaoa.uc.account.bo;

import com.eaoa.base.EABaseBusinessObject;
import com.eaoa.constants.PubAccountConstants;
import com.eaoa.dao.pub.PubAccountDAO;
import com.eaoa.vo.pub.PubAccount;
import com.stony.core.exception.RunException;
import com.stony.core.util.MD5;

public class AccountChangePasswordBO extends EABaseBusinessObject {

	private Long employeeno;
	private String oldpassword;
	private String password;
	private boolean isDefault;

	private PubAccountDAO employeeDAO = new PubAccountDAO();

	public AccountChangePasswordBO(Long employeeno, String oldpassword , String password , boolean isDefault) {
		super();
		this.employeeno = employeeno;
		this.oldpassword = oldpassword;
		this.password = password;
		this.isDefault = isDefault;
	}

	protected void performBusinessLogic() {
		MD5 md5 = new MD5();
		PubAccount po = (PubAccount) employeeDAO.loadAndLock(employeeno);
		
		if(isDefault){
			password = md5.getMD5ofStr(PubAccountConstants.DEFAULT_PASSWORD);
			po.setPassword(password);
			po.setIsfirstlogon("Y");
			po.setPassworderrorscount(new Long(0));
		}else{
			oldpassword = md5.getMD5ofStr(oldpassword);
			password = md5.getMD5ofStr(password);
			
			if(!po.getPassword().equals(oldpassword)){
				throw new RunException("errors.change.oldpassword");
			}
			po.setPassword(password);
			po.setIsfirstlogon("N");
			po.setPassworderrorscount(new Long(0));
		}


		employeeDAO.update(po);
	}

}